﻿using MCPlatform.MotionLib.Base.AssitantClass;
using MCPlatform.MotionLib.Base.Environment;
using MCPlatform.UI.Controls.Models;
using Panuon.WPF.UI;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO.Ports;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MCPlatform.UI.Controls.ViewModels.Communicate;
using Caliburn.Micro;
using MCPlatform.UI.Controls.Views.Communicate;
using MCPlatform.UI.Controls.ViewModels;
using MCPlatform.Communicate.SerialPorts;

namespace MCPlatform.UI.Controls.Views
{
    /// <summary>
    /// SerialPort.xaml 的交互逻辑
    /// </summary>
    public partial class SerialPortControl : UserControl
    {
        public SerialPortControl()
        {
            InitializeComponent();

            //获取端口号
            string[] ArryPort = SerialPort.GetPortNames();
            this.cbo_portName.ItemsSource = ArryPort;

          

        }

        
        public ObservableCollection<int> BaudRates { get; set; } = new ObservableCollection<int> { 4800, 9600, 19200, 115200, 230400 };

        public ObservableCollection<int> DataBits { get; set; } = new ObservableCollection<int> { 5, 6, 7, 8 };

        public ObservableCollection<int> StopBits { get; set; } = new ObservableCollection<int> { 1,2};

        public ObservableCollection<string> Parities { get; set; } = new ObservableCollection<string> { "None", "Odd", "Even", "Mark", "Space" };

        public SerialPort? Conn { get; set; }

        public SerialPortDebugViewModel? SerialPortDebugViewModel { get; set; }

        public SerialPortModel SerialPortModel
        {
            get { return (SerialPortModel)GetValue(SerialPortModelProperty); }
            set { SetValue(SerialPortModelProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SerialPortModel.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SerialPortModelProperty =
            DependencyProperty.Register("SerialPortModel", typeof(SerialPortModel), typeof(SerialPortControl), new PropertyMetadata(default));


        private void Refresh_Click(object sender, RoutedEventArgs e)
        {
            string[] ArryPort = SerialPort.GetPortNames();           
            this.cbo_portName.ItemsSource = ArryPort;
        }

        private void btn_Open_Click(object sender, RoutedEventArgs e)
        {
             Conn = SerialPortModel.SerialPort;
            if (Conn is null)
            {
                return;
            }
            try
            {
                if (Conn.IsOpen)
                {
                    Conn.Close();

                }
                else
                {
                    SetConnInfoFromUI();
                    Conn.Open();
                }

            }
            catch (Exception ex)
            {
                
                Toast.Show(ex.ToString(), targetWindow: ToastWindow.MainWindow);
               
            }
           

            SerialPortModel.IsOpen = Conn.IsOpen;

            SetUIDisable(!Conn.IsOpen);
        }

        private void SetConnInfoFromUI()
        {
            if (Conn is null)
            {
                return;
            }
            Conn.PortName = cbo_portName.Text;
            Conn.BaudRate = int.Parse(cbo_BaudRate.Text);
            Conn.DataBits = int.Parse(cbo_DataBit.Text);
            Conn.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cbo_stopBit.Text); ;
            Conn.Parity = (Parity)Enum.Parse(typeof(Parity), cbo_Parity.Text); ;
        }

        private void SetUIDisable(bool enable)
        {
           cbo_portName.IsEnabled = enable;
           cbo_BaudRate.IsEnabled = enable;
           cbo_DataBit.IsEnabled = enable;
           cbo_stopBit.IsEnabled = enable;
           cbo_Parity.IsEnabled = enable;
        }

        private void btn_save_Click(object sender, RoutedEventArgs e)
        {
            String path = Paths.ComConfigPaths;
            SaveConfigData.SaveConfig<SerialPortModel>(path,this.SerialPortModel);           
        }

        private void btn_Debug_Click(object sender, RoutedEventArgs e)
        {
            if (SerialPortModel is null || !SerialPortModel.IsOpen)
            {
                Toast.Show("串口未打开或者为空！", targetWindow: ToastWindow.MainWindow);
                return;
            }
            var windowManager = IoC.Get<IWindowManager>();
            if (SerialPortDebugViewModel is null)
            {
                //SerialPortDebugViewModel = new SerialPortDebugViewModel(new SerialPortComm(SerialPortModel.SerialPort));
                SerialPortDebugViewModel = new SerialPortDebugViewModel(SerialPortModel.ISerialPort?? new SerialPortComm(SerialPortModel.SerialPort));
            }
                   
            windowManager.ShowDialogAsync(SerialPortDebugViewModel);



        }
    }

   

}
